package com.hdsx.jxcsxm.utile;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
    	//System.out.println("==================");
        // 获得在下面代码中要用的request,response,session对象
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        HttpSession session = servletRequest.getSession();

        // 获得用户请求的URI
        String path = servletRequest.getRequestURI();

        // 从session里取用户信息
        String username = (String) session.getAttribute("truename");
        // 登陆页面无需过滤
        if(path.indexOf("/login.jsp") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (path.indexOf("/urllogin.jsp") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("/urllogin.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("/enterLogin.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("/isLoginByEncryption.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("/getCode.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("/login.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (path.indexOf("/pushDataToHallPlatform.do") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }

        if(path.indexOf(".css") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf(".js") > -1 && path.indexOf(".jsp") == -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (path.indexOf("crypto-js.min.js") != -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf(".jpg") > -1 || path.indexOf(".png") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(path.indexOf("image") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        // 判断如果没有取到员工信息,就跳转到登陆页面
        if (username == null || "".equals(username)) {

            servletResponse.sendRedirect("/jxcsxm/login.jsp");
//            PrintWriter writer = response.getWriter();
//            writer.println("<html>");
//            writer.println("<script>");
//            writer.println("window.open ('/jxcsxm/login.jsp','_top')");
//            writer.println("</script>");
//            writer.println("</html>");
//            return;
            // 跳转到登陆页面
           // System.out.println("http://localhost:8080/jxcsxm/login.jsp");
           // System.out.println("/login.jsp");
            // servletRequest.getRequestDispatcher("http://localhost:8080/jxcsxm/login.jsp").forward(request, response);

            // response.sendRedirect("http://localhost:8080/jxcsxm/login.jsp");
           // servletResponse.sendRedirect("/jxcsxm/login.jsp");
            // servletResponse.sendRedirect("http://localhost:8080/jxcsxm/login.jsp");
        } else {
            // 已经登陆,继续此次请求
            chain.doFilter(request, response);
        }

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

}
